//
//  SegmentViewController.swift
//  DemoProject
//
//  Created by hao on 2018/7/18.
//  Copyright © 2018年 hao. All rights reserved.
//

import UIKit

class SegmentViewController: BaseTableViewController {

    let disposeBag = DisposeBag()
    
    lazy var segmentView: UISegmentedControl = {
        let _view = UISegmentedControl(items: ["第一个", "2", UIImage(named: "btn_next_play") ?? ""])
        self.tableHeadView.addSubview(_view)
        _view.snp.makeConstraints({ (make) in
            make.center.equalTo(self.tableHeadView)
            make.height.equalTo(64)
        })
        return _view
    }()
    
    lazy var label: UILabel = {
        let _label = UILabel()
        self.tableHeadView.addSubview(_label)
        _label.snp.makeConstraints({ (make) in
            make.centerX.equalTo(self.tableHeadView)
            make.bottom.equalTo(segmentView.snp.top).offset(-20)
        })
        return _label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupData()
        segmentView.setWidth(100, forSegmentAt: 2)
        // 默认选中第一个索引的Segment
        segmentView.selectedSegmentIndex = 1
        _rxSwiftProperty()
    }
    
    private func _rxSwiftProperty() {
        segmentView.rx.selectedSegmentIndex.asObservable().subscribe(onNext: { print(info: "当前点击项: \($0)") }).disposed(by: disposeBag)
        
        segmentView.rx.selectedSegmentIndex.asObservable().map({ (index) -> String in
            "当前点击项: \(index)"
        }).bind(to: label.rx.text).disposed(by: disposeBag)
    }
    
    private func setupData() {
        let titles = ["选择控件中选项的添加和删除",
                      "修改选项颜色（包括图片选项）",
                      "修改选项文字",
                      "修改选项图片",
                      "修改选项内容偏移位置",
                      "设置对应Segment的宽度"]
        
        let descArr = ["""
                        // 添加文字选项
                        segmented.insertSegment(withTitle: "新增选项", at:1, animated:true);
                        // 添加图片选项
                        segmented.insertSegment(with: UIImage(named:"icon")!, at:1, animated: true)
                        // 移除选项
                        segmented.removeSegment(at: 1, animated:true)
                       """,
                       "segmented.tintColor = UIColor.red",
                       "segmented.setTitle(\"swfit\", forSegmentAt:1)",
                       """
                        （1）下面代码图片会自动渲染成蓝色
                        segmented.setImage(UIImage(named:"icon"), forSegmentAt:2)

                        （2）使用下面代码，图标保留原来的颜色
                        segmented.setImage(UIImage(named:"icon")?.withRenderingMode(.alwaysOriginal), forSegmentAt:2)
                       """,
                       "segmented.setContentOffset(CGSize(width:10, height:7), forSegmentAt:1)",
                       "segmentView.setWidth(100, forSegmentAt: 2)",]
        
        addTitles(titles, footDescs: descArr)
    }

}
